package com.amadeus.spin.util.soapshooter;

import com.amadeus.spin.standard.util.HTMLEntityDecoder;

public class SoapClientException extends ShooterException {

  /**
   *
   */
  private static final long serialVersionUID = 1L;

  private final Throwable detail;

  /**
   * Constructs a <code>SoapClientException</code> with the specified detail
   * message.
   *
   * @param s
   *          the detail message
   */
  public SoapClientException(String s) {
    super(s);
    detail = null;
  }

  /**
   * Constructs a <code>SoapClientException</code> with the specified detail
   * message and nested exception.
   *
   * @param s
   *          the detail message
   * @param ex
   *          the nested exception
   */
  public SoapClientException(String s, Throwable ex) {
    super(s);
    detail = ex;
  }

  /**
   * Returns the detail message, including the message from the nested exception
   * if there is one.
   *
   * @return The message value
   */
  @Override
  public String getMessage() {
    String msg = "";
    if (detail == null) {
      msg = super.getMessage();
    }
    else {
      msg = super.getMessage() + "\nNested exception: \n" + detail.getMessage();
      if (detail.getCause() != null) {
        msg += "\nCause: " + detail.getCause().getMessage();
      }
    }
    HTMLEntityDecoder entityDecoder = new HTMLEntityDecoder();
    return entityDecoder.decode(msg);
  }

  /**
   * Prints the composite message and the embedded stack trace to the specified
   * stream <code>ps</code>.
   *
   * @param ps
   *          the print stream
   */
  @Override
  public void printStackTrace(java.io.PrintStream ps) {
    if (detail == null) {
      super.printStackTrace(ps);
    }
    else {
      synchronized (ps) {
        ps.println(this);
        // Redundant data
        // detail.printStackTrace(ps);
      }
    }
  }

  /**
   * Prints the composite message to <code>System.err</code>.
   */
  @Override
  public void printStackTrace() {
    printStackTrace(System.err);
  }

  /**
   * Prints the composite message and the embedded stack trace to the specified
   * print writer <code>pw</code>.
   *
   * @param pw
   *          the print writer
   */
  @Override
  public void printStackTrace(java.io.PrintWriter pw) {
    if (detail == null) {
      super.printStackTrace(pw);
    }
    else {
      synchronized (pw) {
        pw.println(this);
        detail.printStackTrace(pw);
      }
    }
  }
}
